package com.bank.service.impl;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.log;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import com.bank.code.RespCode;
import com.bank.dao.CustomerInfoMapper;
import com.bank.entity.CustomerInfo;
import com.bank.exception.CommonException;
import com.bank.service.CustomerService;
import com.bank.util.ImportExeclUtil;


@Service(value="customerService")
public class CustomerServiceIml implements CustomerService {

	Logger logger = Logger.getLogger(CustomerServiceIml.class);
	
	@Autowired
	CustomerInfoMapper  customerInfoMapper;
	
	public void addCustomer(Map<String, Object> params) throws CommonException{
		
		String customerIdno = (String)params.get("customer_idno");
		String customerIdType  =  (String)params.get("customer_id_type");
		Map<String,Object> paramTmp = new HashMap<String, Object>();
		paramTmp.put("customer_idno", customerIdno);
		paramTmp.put("customer_id_type", customerIdType);
		List<CustomerInfo> customerInfoList=customerInfoMapper.selectByIdno(paramTmp);
		if(customerInfoList.size()!=0){
			logger.debug("客户已存在");
			throw  new CommonException(RespCode.ACCT_EXISTS, "客户已存在");
		}
		
		
		
		int maxNumber  = customerInfoMapper.findCustomerMaxNumber(paramTmp);
		logger.debug("maxNumber="+maxNumber);
		String number = String.valueOf(maxNumber+1);
		CustomerInfo customerInfo = new CustomerInfo();
		customerInfo.setCustomer_number(number);
		customerInfo.setCustomer_name((String)params.get("customer_name"));
		customerInfo.setCustomer_type((String)params.get("customer_type"));
		customerInfo.setCustomer_id_type((String)params.get("customer_id_type"));
		customerInfo.setCustomer_idno((String)params.get("customer_idno"));
		customerInfo.setCustomer_phone((String)params.get("customer_phone"));
		customerInfo.setCustomer_email((String)params.get("customer_email"));
		customerInfo.setCustomer_address((String)params.get("customer_address"));
		customerInfo.setCustomer_ori((String)params.get("customer_ori"));
		customerInfoMapper.insertSelective(customerInfo);
	}

	public CustomerInfo queryCustomer(String number) {
		return customerInfoMapper.selectByPrimaryKey(number);
	}

	public void updateCustomer(Map<String, Object> params) throws CommonException {
		String number = (String)params.get("customer_number");
		CustomerInfo customerInfoResult = this.getCustomerInfoByNumber(number);
		if(customerInfoResult==null){
			throw  new CommonException(RespCode.ACCT_NOT_EXISTS, "账号不存在");
		}
		CustomerInfo customerInfo = new CustomerInfo();
		customerInfo.setCustomer_number(number);
		customerInfo.setCustomer_name((String)params.get("customer_name"));
		customerInfo.setCustomer_type((String)params.get("customer_type"));
		customerInfo.setCustomer_id_type((String)params.get("customer_id_type"));
		customerInfo.setCustomer_idno((String)params.get("customer_idno"));
		customerInfo.setCustomer_phone((String)params.get("customer_phone"));
		customerInfo.setCustomer_email((String)params.get("customer_email"));
		customerInfo.setCustomer_address((String)params.get("customer_address"));
		customerInfo.setCustomer_ori((String)params.get("customer_ori"));
		logger.debug("开始更新数据");
		customerInfoMapper.updateByPrimaryKeySelective(customerInfo);
		
	}

	public List<Object> queryCustomerList(Map<String, Object> params) {
		
		return null;
	}
	
	public CustomerInfo getCustomerInfoByNumber(String number){
		return customerInfoMapper.selectByPrimaryKey(number);
	}
	
	/**
	 * 批量导入客户信息
	 * @throws Exception 
	 * */
	 public int batchImport( MultipartFile file_name){
		 try {
			 	int sum = 0;
		 		List<List<Object>> listob = null;  
		 		InputStream in=file_name.getInputStream();
		 		listob = new ImportExeclUtil().getWorkListByExecl(in, file_name);
		 		in.close();
		 		logger.debug("size="+listob.size());
		 		//该处可调用service相应方法进行数据保存到数据库中，现只对数据输出
		        for (int i = 0; i < listob.size(); i++) {
		        	sum++;
		            List<Object> lo = listob.get(i);
		            logger.debug("lo.size:"+lo.size());
		            CustomerInfo customer = new CustomerInfo();
		            customer.setCustomer_number(String.valueOf(lo.get(0)));
		            customer.setCustomer_name(String.valueOf(lo.get(1)));
		            customer.setCustomer_id_type(String.valueOf(lo.get(2)));
		            customer.setCustomer_idno(String.valueOf(lo.get(3)));
		            customer.setCustomer_phone(String.valueOf(lo.get(4)));
		            customer.setCustomer_email(String.valueOf(lo.get(5)));
		            customer.setCustomer_address(String.valueOf(lo.get(6)));
		            customerInfoMapper.insertSelective(customer);
		        }
		        return sum;

		} catch (Exception e) {
			return -1;
		}
	 }

	 /**
	 * 根据证件号和证件类型获取客户信息
	 * @throws CommonException 
	 * @throws Exception 
	 * */
	public CustomerInfo selectByIdno(Map<String, Object> param) throws CommonException {
		String customerIdno = (String)param.get("customer_idno");
		String customerIdType  =  (String)param.get("customer_id_type");
		Map<String,Object> paramTmp = new HashMap<String, Object>();
		paramTmp.put("customer_idno", customerIdno);
		paramTmp.put("customer_id_type", customerIdType);
		List<CustomerInfo> customerInfoList=customerInfoMapper.selectByIdno(paramTmp);
		if(customerInfoList.size()==0){
			logger.debug("客户不存在");
			throw  new CommonException(RespCode.CUST_NOT_EXISTS, "客户不存在");
		}
		CustomerInfo customer  = null;
		for(CustomerInfo customerTmp:customerInfoList){
			customer = customerTmp;
		}
		return customer;
	}
	 
	 
	 
	 
	 
	 
}
